package cn.momoshenchi.medicine.service.impl;

import cn.momoshenchi.medicine.vo.HospitalByDept;
import cn.momoshenchi.medicine.vo.SimpDoctor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.medicine.pojo.BaseDoctor;
import cn.momoshenchi.medicine.service.BaseDoctorService;
import cn.momoshenchi.medicine.mapper.BaseDoctorMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class BaseDoctorServiceImpl extends ServiceImpl<BaseDoctorMapper, BaseDoctor>
        implements BaseDoctorService
{

    @Override
    public List<HospitalByDept> getByOrgIdOrderByDept(String orgId)
    {

        List<BaseDoctor> baseDoctorList = list(new QueryWrapper<BaseDoctor>().eq("org_id", orgId));

        //对医生列表按照dept_id 分组
        //1.先根据dept_id排序
        List<BaseDoctor> org = baseDoctorList.stream().sorted(Comparator.comparing(BaseDoctor::getDeptId)).collect(Collectors.toList());
        List<HospitalByDept> hospitalByDeptList = new ArrayList<>();

        //2.然后遍历集合,判断前后id是否一致,来决定创建HospitalByDept
        List<SimpDoctor> simpDoctorList = null;
        for (int i = 0; i < org.size(); i++)
        {
            SimpDoctor simpDoctor = new SimpDoctor();
            BeanUtils.copyProperties(org.get(i), simpDoctor);
            if (i == 0 || (!org.get(i).getDeptId().equals(org.get(i - 1).getDeptId())))
            {
                simpDoctorList = new ArrayList<>();
                HospitalByDept hospitalByDept = new HospitalByDept();
                hospitalByDept.setDeptName(org.get(i).getDeptName());
                hospitalByDept.setDoctors(simpDoctorList);
                hospitalByDeptList.add(hospitalByDept);
            }
            simpDoctorList.add(simpDoctor);
        }

        return hospitalByDeptList;

    }
}




